import { routes, asyncRoutes } from '@/router'
//判断是否有权限访问该菜单
function hasPermission(menus: any, route: any) {
    if (route.name) {
        let currMenu = getMenu(route.name, menus);
        if (currMenu != null) {
            //设置菜单的标题、图标和可见性
            if (currMenu.title != null && currMenu.title !== '') {
                route.meta.title = currMenu.title;
            }
            if (currMenu.icon != null && currMenu.title !== '') {
                route.meta.icon = currMenu.icon;
            }
            if (currMenu.hidden != null) {
                route.hidden = currMenu.hidden !== 0;
            }
            if (currMenu.sort != null && currMenu.sort !== '') {
                route.sort = currMenu.sort;
            }
            return true;
        } else {
            route.sort = 0;
            //不在左侧菜单显示的路由，也需要获取出来，用来动态注册
            if (route.hidden !== undefined && route.hidden === true) {
                route.sort = -1;
                return true;
            } else {
                return false;
            }
        }
    } else {
        // 通用页面一般没有name属性，但也不需要进行权限控制
        return true
    }
}
function getMenu(name:any, menus:any) {
    for (let i = 0; i < menus.length; i++) {
        let menu = menus[i];
        if (name === menu.name) {
            return menu;
        }
    }
    return null;
}

//对菜单进行排序
function sortRouters(accessedRouters:any) {
    for (let i = 0; i < accessedRouters.length; i++) {
        let router = accessedRouters[i];
        if (router.children && router.children.length > 0) {
            router.children.sort(compare("sort"));
        }
    }
    accessedRouters.sort(compare("sort"));
}

//降序比较函数
function compare(p:any) {
    return function (m:any, n:any) {
        let a = m[p];
        let b = n[p];
        return b - a;
    }
}
export default {
    namespaced:true,
    state: {
        routes,
        asyncRoutes,
    },
    mutations: {
        SET_ROUTES(state: any, data: any) {
            state.routes = data;
        },
        SET_ASYNCROUTES(state: any, data: any) {
            state.asyncRoutes = data;
        }
    },
    actions: {
        GenerateRoutes({ commit }:any, data:any) {
           
              const { menus } = data;
              const accessedRouters = asyncRoutes.filter(v => {
                //admin帐号直接返回所有菜单
                // if(username==='admin') return true;
                if (hasPermission(menus, v)) {
                  if (v.children && v.children.length > 0) {
                    v.children = v.children.filter(child => {
                      if (hasPermission(menus, child)) {
                        return child
                      }
                      return false;
                    });
                    return v
                  } else {
                    return v
                  }
                }
                return false;
              });
              //对菜单进行排序
              sortRouters(accessedRouters);
              commit('SET_ROUTES', routes.concat(accessedRouters));
              commit('SET_ASYNCROUTES', asyncRoutes);
          }
    }
}